package com.example.lwx.contentProvider;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.example.lwx.R;

public class ContentObserverActivity extends AppCompatActivity {

    TextView tv_observer;
    Button bt_observer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.observer___8);
        tv_observer=findViewById(R.id.tv_observer);
        bt_observer=findViewById(R.id.bt_observer);
        bt_observer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(ContextCompat.checkSelfPermission(ContentObserverActivity.this, Manifest.permission.READ_SMS)!= PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(ContentObserverActivity.this, new String[]{Manifest.permission.READ_SMS}, 1);
                }else {
                    ObserverSms();
                }
            }
        });
    }

    class SmsOberver extends ContentObserver{
        @Override
        public void onChange(boolean selfChange) {
            //super.onChange(selfChange);
            Cursor cursor=getContentResolver().query(Uri.parse("content://sms/"),null,null,null,null);
            while(cursor.moveToNext()){
                @SuppressLint("Range") String phone=cursor.getString(cursor.getColumnIndex("address"));
                @SuppressLint("Range") String content=cursor.getString(cursor.getColumnIndex("body"));
                tv_observer.append("收件人："+phone+"\n内容："+content+"\n\n");
            }
            cursor.close();
        }
        public SmsOberver(Handler handler) {
            super(handler);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode==1){
            if(grantResults.length>0&&grantResults[0]== PackageManager.PERMISSION_GRANTED){
                ObserverSms();
            }else {
                Toast.makeText(ContentObserverActivity.this,"权限申请失败",Toast.LENGTH_SHORT).show();
            }
        }
    }
    private void ObserverSms() {
        Uri uri= Uri.parse("content://sms/");
        ContentResolver contentResolver=getContentResolver();
        contentResolver.registerContentObserver(uri,true,new SmsOberver(new Handler()));
    }
}














